/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.plugins.impl;

import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.grayrabbit.cms.constant.DictionaryStaticValues;
import org.grayrabbit.cms.dao.plugins.SurveyDao;
import org.grayrabbit.cms.entity.Survey;
import org.grayrabbit.cms.vo.plugins.SurveyBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>Survey</b> dao implementation 





 * @version 1.0，创建时间：2011-05-16 16:08:59 
 */
@Repository
public class SurveyDaoImpl extends GenericHibernateDao<Survey, String>
		implements SurveyDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From Survey as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see SurveyDao#assembly(SurveyBean)
	 */
	public QueryCondition assembly(SurveyBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see SurveyDao#assemblyCount(SurveyBean)
	 */
	public QueryCondition assemblyCount(SurveyBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see SurveyDao#count(SurveyBean)
	 */
	public int count(SurveyBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see SurveyDao#assemblyNameQuery(SurveyBean)
	 */
	public QueryCondition assermblyNameQuery(SurveyBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see SurveyDao#assemblyNameQueryCount(SurveyBean)
	 */
	public QueryCondition assemblyNameQueryCount(SurveyBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see SurveyDao#findByPage(Page, SurveyBean)
	 */
	public List<Survey> findByPage(Page<Survey> page,
			SurveyBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		qc.append(" order by obj.ord desc");
		return findByQc(qc);
	}
	
	public List<Survey> ListAll(SurveyBean bean){
		QueryCondition qc = assembly(bean);
		return findByQc(qc);
	}
	// 业务查询主要逻辑代码
	private void transNameQuery(SurveyBean bean,QueryCondition qc){
		if(bean!=null){
			if(StringUtils.isNotBlank(bean.getSurveyTitle())){
				qc.add("And obj.surveyTitle like ? ","%"+ bean.getSurveyTitle()+"%");
			}
			if(StringUtils.isNotBlank(bean.getCreateRealName())){
					qc.add("And obj.createRealName like ? ", bean.getCreateRealName()+"%");
				}
            if(bean.getSurveyStatus() != null){
				qc.add("And obj.surveyStatus = ? ", bean.getSurveyStatus());
			}
         
            if(StringUtils.isNotBlank(bean.getSite().getSiteId())){
            	qc.add("And obj.site.siteId = ? ", bean.getSite().getSiteId());
            }

		}
	}
	/**
	 * 检查唯一性
	 */
	public Boolean checkNameUnique(String surveyTitle,String surname,String siteId){
		boolean unique=false;
		List<Survey> survetList=null;
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(StringUtils.isNotBlank(surveyTitle)){
			qc.add(" And obj.surveyTitle =?", surveyTitle);
		}
		if(StringUtils.isNotBlank(siteId)){
			qc.add(" And obj.site.siteId = ?", siteId);
		}
		survetList=findByQc(qc);
		if(StringUtils.isNotBlank(surname) && !survetList.isEmpty()){
			for(int i=0;i<survetList.size();i++){
				if(survetList.get(i).getSurveyTitle().equals(surname)){
					survetList.remove(i);
				}
			}
		}
		if((survetList!=null)&&(!survetList.isEmpty())){
			unique=true;
		}
		return unique;
	}
	public List<Survey> findPublishBySiteId(String siteId,
			int currentSurveryIndex, int max){
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(StringUtils.isNotBlank(siteId)){
			qc.add(" And obj.site.siteId = ?", siteId);
		}
		qc.add("And obj.surveyStatus = ?", DictionaryStaticValues.SURVEYSTATUS_ACTIVE);
		qc.setMaxResults(max);
		qc.setOffset(currentSurveryIndex);
		qc.append("Order by obj.surveyId desc");
		return findByQc(qc);
	}
	public List<Survey> surList(){
		List<Survey> list = null;
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.append(" order by obj.ord desc");
		list = findByQc(qc);
		return list;
	}
	/**
	 * 根据站点ID查询
	 * @return
	 */
	public List<Survey>findBySiteId(String siteId){
		QueryCondition qc = assembly(null);
		qc.add("And obj.site.siteId = ? ", siteId);
		return findByQc(qc);
	}
}